package com.xcw.bos.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.xcw.bos.domain.Workordermanage;
import com.xcw.bos.web.action.base.BaseAction;

/**
 * 工作单操作Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class WorkordermanageAction extends BaseAction<Workordermanage>{
	@Resource
	private ProcessEngine processEngine;
    //保存工作单
	public String save() throws IOException{
		String f = "1";
		try {
			workordermanageService.save(model);
		} catch (Exception e) {
			f = "0";
		}
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(f);
		return NONE;
	}
	
	// 查询状态为“未启动”的工作单
	public String list(){
		List<Workordermanage> list = workordermanageService.findNotStart();
		//Set set = new HashSet();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	
	// 启动物流配送流程
	public String start(){
		String processDefinitionKey = "transfer";// 物流配送流程
		String businessKey = model.getId();
		Map<String, Object> variables = new HashMap<String, Object>();
		Workordermanage workordermanage = workordermanageService.findById(model.getId());
		variables.put("业务数据", workordermanage);
		processEngine.getRuntimeService().startProcessInstanceByKey(
				processDefinitionKey, businessKey, variables);
		workordermanage.setStart("1");
		workordermanageService.update(workordermanage);
		return "toList";
	}
}
